package com.siyuanren.gcocp.web;

import cn.hutool.core.io.FileUtil;
import com.siyuanren.gcocp.common.config.PublicConfig;
import com.siyuanren.gcocp.common.web.bean.Result;
import com.siyuanren.gcocp.dto.UserDTO;
import com.siyuanren.gcocp.dto.WxCategoryDTO;
import com.siyuanren.gcocp.dto.WxCourseDTO;
import com.siyuanren.gcocp.dto.wx.WxUserInviteCourseDTO;
import com.siyuanren.gcocp.entity.*;
import com.siyuanren.gcocp.mapper.MyWxActiveMapper;
import com.siyuanren.gcocp.mapper.MyWxCourseMapper;
import com.siyuanren.gcocp.mapper.MyWxUserVoucherMapper;
import com.siyuanren.gcocp.mapper.base.WxUserVoucherMapper;
import com.siyuanren.gcocp.service.*;
import com.siyuanren.gcocp.service.impl.OrderServiceImpl;
import com.siyuanren.gcocp.web.wx.BaseWxController;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.web
 * Date         2018/6/12
 * Time         13:30
 * Description  测试用controller
 */

@Slf4j
@Controller
@RequestMapping("/test")
public class TestController extends BaseWxController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserVoucherService userVoucherService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private MyWxUserVoucherMapper userVoucherMapper;
    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private MyWxCourseMapper courseMapper;
    @Autowired
    private MyWxActiveMapper activeMapper;
    @Autowired
    private TaskService taskService;

    /**
     * 得到用户信息
     * @return
     */
    @ResponseBody
    @RequestMapping("completeActive")
    public Result completeActive(Integer activeId) throws WxErrorException, IOException {
        WxUserVoucher userVoucher = userVoucherMapper.selectByPrimaryKey(activeId);
        orderService.activeComplete(userVoucher);
        return new Result();
    }


    @ResponseBody
    @RequestMapping("verifyPicExist")
    public Result verifyPicExist() throws Exception {
        StringBuffer sb = new StringBuffer();
        List<WxActive> wxActives = activeMapper.selectByExample(new WxActiveExample());
        for (WxActive wxActive : wxActives) {
            File bg = FileUtil.file(PublicConfig.RESOURCE_PATH + wxActive.getBackgroundImg());
            File sponsor = FileUtil.file(PublicConfig.RESOURCE_PATH + wxActive.getSponsorVoucherImg());
            if (!bg.exists()) {
                sb.append("id=").append(wxActive.getId());
                sb.append(",name=").append(wxActive.getActiveName());
                sb.append(",").append(wxActive.getBackgroundImg()).append(" is not exist.");
                sb.append(bg.getPath());
            }
            if (!sponsor.exists()) {
                sb.append("id=").append(wxActive.getId());
                sb.append(",name=").append(wxActive.getActiveName());
                sb.append(",").append(wxActive.getSponsorVoucherImg()).append(" is not exist.");
                sb.append(sponsor.getPath());
            }
        }

        return new Result(sb);
    }

    @RequestMapping("sendWxMsg")
    @ResponseBody
    public void sendWxMsg() {
        taskService.studyMorningRemind();
    }

}
